package com.zkca.xf.presenter;

import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.zkca.xf.base.BasePresenter;
import com.zkca.xf.manager.DataManager;
import com.zkca.xf.model.javabean.UserId;
import com.zkca.xf.view.FeedbackView;

import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DefaultObserver;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by pc on 2018/8/15.
 */

public class FeedbackPresenter  extends BasePresenter {

    private DataManager dataManager;
    private FeedbackView feedbackView;

    public FeedbackPresenter(LifecycleProvider<ActivityEvent> provider, FeedbackView feedbackView) {
        super(provider);
        this.feedbackView = feedbackView;
        dataManager = DataManager.getInstance();
    }


    public void Feedback(Map<String, String> map) {
        dataManager.FeedBack(map)
                .subscribeOn(Schedulers.io())// 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread())// UI线程处理返回接口
                .compose(getProvider().<UserId>bindUntilEvent(ActivityEvent.DESTROY))// onDestroy取消订阅
                .subscribe(new DefaultObserver<UserId>() {// 订阅
                    @Override
                    public void onNext(UserId userId) {
                        feedbackView.getFeedback(userId);
                    }

                    @Override
                    public void onError(Throwable e) {
                        feedbackView.showError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        feedbackView.hideProgressDialog();
                    }
                });
    }
}

